

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
};

//function btnStyleJqueryDialog(buttonLabel) {
//    $('.ui-dialog-buttonpane :button').each(function() {
//        if ($(this).text() == buttonLabel) { 
//            $(this).addClass('naranja'); 
//        }
//    });
//}
jQuery.fn.reset = function () {
  $(this).each (function() {this.reset();});
}

function validateNumeric(event) {
    // Backspace, tab, enter, end, home, left, right, del
    var controlKeys = [8, 9, 13, 35, 36, 37, 39, 46];
    var isControlKey = controlKeys.join(',').match(new RegExp(event.which));
    //console.log('isControlKey ' + isControlKey);
    if (!event.which 
        || (48 <= event.which && event.which <= 57) // 0 a 9
        || isControlKey) {
        return;
    } else {
        event.preventDefault();
    }
}

function esCampoNumerico(numero){                     
      var re = /^(-)?[0-9]*$/;
      if (!re.test(numero)) {
        return false;
     }else{
         return true;
     }
}

function esSoloLetras(word){
    if(/[0-9]/.test(word)){
        return true;  
    }else{
        return false;
    } 
}




function limpiarForm() {
    if (document && document.forms && document.forms[0]) {
        document.forms[0].reset();
    }
}


function limpiarCombo(idCombo,labelInicial) {
    $(idCombo).children().remove();
    $(idCombo).html('<option selected value="-1">' + labelInicial + '</option>');
}

// el objeto a mostrar debe tener propiedades id y nombre
function cargarCombo(idCombo, listado,labelInicial,itemId,itemLabel) {
    limpiarCombo(idCombo,labelInicial);
    if (listado) {
        var options ="";//$(idCombo).html();
        var data=null;
        for (var i = 0; i < listado.length; i++) {
            data=listado[i];
            options += '<option value="' + data[itemId] + '">' + data[itemLabel] + "</option>";
        }
        //$(idCombo).html(options);
        if (document.getElementById(idCombo))
            document.getElementById(idCombo).innerHTML=options;
    }
}

// en caso de no ejecutar llamada ajax, nextURL=variable sin valor (undefined) y datajson={}
function Confirmar(mensaje, nextURL,dataJSON,aceptarFunction,cancelarFunction) {
//se necesita este div en el jsp
//<div id="dialog-confirm" title="Confirmar" style="display: none;"><p id="popupConfirm"></p></div>
    $('#dialog-confirm')
    .dialog({
            autoOpen: false,
            modal: true,
            draggable: false,
            resizable : false,
            buttons: {
                No: function(){
                        $(this).dialog("destroy");
                        if (cancelarFunction)
                                cancelarFunction();
                },
                 Sí: function() {
                        $( this ).dialog( "destroy" );
                        if (nextURL!=undefined){
                            $.getJSON(nextURL, dataJSON, function(response, textStatus) {
                                if (!response.error) {
                                           aceptarFunction();
                                } else {
                                    alert(response.error);
                                }
                            });
                        }else{
                             if (aceptarFunction)
                                aceptarFunction();
                        }
                }
            },
             open: function() {
                $(this).parents('.ui-dialog-buttonpane button:eq(1)').focus();//aceptar
                $(this).parents('.ui-dialog-buttonpane button:eq(0)').blur();//cancelar
            }
    });


    $( "#popupConfirm" ).html(mensaje);

    $( "#dialog-confirm" ).bind( "dialogclose", function(event, ui) {});

    $( "#dialog-confirm" ).dialog( "open" );

}

//exito y redireccion
function mensajeExito(mensaje, nextURL,isPopup,funcAceptar) {

    $('<div id="dialog-exito" title="Éxito" style="display: none;"><p id="popupExito"></p></div>')
    .dialog({
        autoOpen: false,
        modal: true,
        draggable: false,
        buttons: {
            Aceptar: function() {
                $( "#dialog-exito" ).dialog("close");

                if (nextURL!=undefined){
                    if (isPopup){
                        window.top.location = Tesis.contextPath + nextURL;
                    }else{
                        window.location = Tesis.contextPath + nextURL;
                    }
                }else{
                    if (funcAceptar)
                        funcAceptar();
                }
            //return false;
            }
        }
    });


    $( "#popupExito" ).html(mensaje);

    $( "#dialog-message" ).bind( "dialogclose", function(event, ui) {});

    $( "#dialog-exito" ).dialog( "open" );

}

//mensaje alerta y redireccion
function mensajeAdvertencia(mensaje, nextURL) {

    $('<div id="dialog-alerta" title="Advertencia" style="display: none;"><p id="popupAlerta"></p></div>')
    .dialog({
        autoOpen: false,
        modal: true,
        draggable: false,
        buttons: {
            Aceptar: function() {
                $( "#dialog-alerta" ).dialog("close");

                if (nextURL!=undefined){
                   window.location = Tesis.contextPath + nextURL;
                }
            //return false;
            }
        }
    });


    $( "#popupAlerta" ).html(mensaje);

    $( "#dialog-message" ).bind( "dialogclose", function(event, ui) {});

    $( "#dialog-alerta" ).dialog( "open" );

}

//crea dialogo tipo alerta
//mensaje: mensaje de validacion
//idFocus ejm: "#idCampo" para darle foco
function mensajeValidacion(mensaje,idFocus) {

    $('<div id="dialog-message" title="Advertencia" style="display: none;"><p id="popupMessage"></p></div>')
    .dialog({
        autoOpen: false,
        modal: true,
        draggable: false,
        buttons: {
            Aceptar: function() {
                $( "#dialog-message" ).dialog("close");
            }
        }
    });


    $( "#popupMessage" ).html(mensaje);
    if (idFocus != undefined && idFocus != null) {
        $( "#dialog-message" ).bind( "dialogclose", function(event, ui) {
            $(idFocus).focus();
        });
    } else {
        $( "#dialog-message" ).bind( "dialogclose", function(event, ui) {
            });
    }
    $( "#dialog-message" ).dialog( "open" );
}


function isValidURL(url){
    var regExpression = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
    return regExpression.test(url);
}

function isValidEmail(email){
    //var regex=/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
    var regex=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return regex.test(email);
}


function validarTamanho(idInput, tamanho) {
        $(idInput).keypress(function(event) {
            if ($(this).val().length > tamanho) {
                event.preventDefault();
                $(this).val($(this).val().substring(0,tamanho));
            }
        });
        $(idInput).keyup(function(event) {
            if ($(this).val().length > tamanho) {
                event.preventDefault();
                $(this).val($(this).val().substring(0,tamanho));
            }
        });
        $(idInput).blur(function(event) {
            if ($(this).val().length > tamanho) {
                event.preventDefault();
                $(this).val($(this).val().substring(0,tamanho));
            }
        });
}

var fillEmptyColumn = function(liner, record, column, data) {
   var linerElement = new YAHOO.util.Element(liner);
    if (!data) {
        linerElement.get('element').innerHTML = '---';
    }else{
        linerElement.get('element').innerHTML = data;
    }
}

function recortarCadena(cadena, longitud) {
    if (cadena.length > longitud + 3) { // 3 porque se concatena el '...'
        return cadena.substring(0, longitud) + '...';
    } else {
        return cadena;
    }
}



//Create jsColor object
//var col = new jsColor("#0081C2");
var col1 = new jsColor("black");
var col2 = new jsColor("#C4C4C4");

//Create jsPen object
var pen1 = new jsPen(col1,4);
var pen2 = new jsPen(col2,4);
                

function linea(gr,p1,p2,id,esAlterna){
    //console.log(p1);
    //console.log(p2.y);
   
    var pt1 = new jsPoint(p1.x,p1.y);
    var pt2 = new jsPoint(p2.x,p2.y);
    var l=null;
    if (esAlterna=='Y')
        l=gr.drawLine(pen2,pt1,pt2);
    else
        l=gr.drawLine(pen1,pt1,pt2);
    if (id==null || id ==undefined){
        l.id="l"+new Date().getTime(); 
    }else
        l.id=id;
    $("#"+l.id).addClass("linea");
    var medio=Math.round($("#"+l.id+" > div").size()/2);
    if (medio==1)//para evitar fuera de index en linea recta
        medio=0;
    $("#"+l.id).children()[medio].style.overflow='visible';
    if (p2.x>p1.x)//esta invertido xD
        $("#"+l.id).children()[medio].innerHTML="<div class='arrow-right'></div>"
    else{
        $("#"+l.id).children()[medio].innerHTML="<div class='arrow-left'></div>"
    }
    //$("#"+l.id).children()[medio].className="arrow-right"
    return l.id;
}

function mostrarNotificacion(mensaje,op){
    $("#notificacion").html('<p align="center">'+mensaje+'</p>');
    if (op=='A'){
        $("#notificacion").show('fade',function(){
            $("#notificacion").hide('fade',{direction:'up'},700);
        });
    }
    if (op=='S'){
        $("#notificacion").show('fade',{direction:'down'},500);
    }
    if (op=='H'){
        $("#notificacion").hide('fade',{direction:'down'},700);
    }
}

function conectar(gr,idIni,idFin,idLinea,esAlterna){
    //var el = new YAHOO.util.Element(idIni);
    var p1={};
    var p2={};

    /*if(el.getStyle('left')){
        p1.x=el.getStyle('left').replace("px","");
        p1.y=el.getStyle('top').replace("px","");
        p1.x=parseInt(p1.x)+16;
        p1.y=parseInt(p1.y)+((idIni.substr(0,1))*32);
    }
    el = new YAHOO.util.Element(idFin);
    if(el.getStyle('left')){
        p2.x=el.getStyle('left').replace("px","");
        p2.y=el.getStyle('top').replace("px","");
        p2.x=parseInt(p2.x)+16;
        p2.y=parseInt(p2.y)+((idFin.substr(0,1))*32);
    }*/

    p1=$("#"+idIni).position();
    p1.x=p1.left+16;
    p1.y=p1.top+32;

    p2=$("#"+idFin).position();
    p2.x=p2.left+16;
    p2.y=p2.top+32;

    var l=linea(gr,p1,p2,idLinea,esAlterna);
    return l;
}
